package com.zhitan.ems.third.dahua.tools;

import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.http.IccTokenResponse;
import com.zhitan.ems.third.dahua.OauthApi;
import com.zhitan.ems.third.dahua.api.video.realTimePreview.RealTimePreviewApi;
import com.zhitan.ems.third.dahua.model.video.realTimePreview.HlsUrlRequest;
import com.zhitan.ems.third.dahua.model.video.realTimePreview.HlsUrlResponse;

public class DahuaKit {
	public static String getShowUrlByCode(IccTokenResponse.IccToken iccToken,
			 RealTimePreviewApi realTimePreviewApi,
			String deviceCode) {
		if(deviceCode==null) {
			return null;
		}
		if(!deviceCode.contains("$")) {
			deviceCode=deviceCode+"$1$0$0";
		}
		HlsUrlRequest.Data hData = new HlsUrlRequest.Data();
		hData.setChannelId(deviceCode);
		hData.setStreamType("1");
		hData.setType("hls");
		HlsUrlRequest hlsUrlRequest=new HlsUrlRequest();
		hlsUrlRequest.setData(hData);
		HlsUrlResponse hlsUrlResponse=realTimePreviewApi.getHlsUrl(hlsUrlRequest);
		String rtspUrl=null;
		if(hlsUrlResponse!=null&&hlsUrlResponse.getData()!=null&&hlsUrlResponse.getData().getUrl()!=null
				&&iccToken!=null&&iccToken.getAccess_token()!=null) {
			try {
				rtspUrl = hlsUrlResponse.getData().getUrl() + "?token=" + iccToken.getAccess_token();
				if(rtspUrl.contains("172.16.0.20")) {//112.8.199.54:7086
					rtspUrl=rtspUrl.replace("172.16.0.20", "112.8.199.54");
				}
			} catch (Exception e) {
			} 
		}
		return rtspUrl;
	}
	public static String getShowUrlByCode(
			String deviceCode) {
		  OauthApi auApi=new OauthApi();
	  	  IccTokenResponse.IccToken iccToken=null;
	  	  RealTimePreviewApi realTimePreviewApi = new RealTimePreviewApi();
	  	  try {
				iccToken= auApi.getTokenInfo();
				return getShowUrlByCode(iccToken, realTimePreviewApi, deviceCode);
			} catch (ClientException e) {
			}
		   return null;
	}
}
